package controllers;

import java.util.List;

import models.*;

import play.data.validation.Required;
import play.modules.search.*;
import play.mvc.*;

@With(Secure.class)
public class SearchController extends Controller {
    
	//TODO: Remove Search-Debugging
    public static void search(@Required String querystr) throws Throwable {    	
    	//Query all courses
    	Query courseQuery = Search.search("professor:\"" + querystr + 
    										"\" OR name:\""+ querystr +
    										"\" OR room:\""+ querystr +
    										"\" OR description:\""+ querystr +
    										"\"", Course.class); 
    	
    	//Query all lectures
    	Query lectureQuery = Search.search("name:\"" + querystr +
    										"\"", Lecture.class); 
    	
    	//Query all comments
    	Query commentQuery = Search.search("content:\"" + querystr +
    										"\" OR postedBy:\"" + querystr +
    										"\"", Comment.class);
    	
    	//Query all users
    	Query userQuery = Search.search("name:\"" + querystr +
										"\" OR realname:\"" + querystr +
										"\" OR email:\"" + querystr +
										"\"", User.class);
    	
    	//Query all questions
    	Query questionQuery = Search.search("title:\"" + querystr +
    										"\" OR content:\"" + querystr +
    										"\"", Question.class);
    	
    	//Query all answers
    	Query answerQuery = Search.search("title:\"" + querystr +
											"\" OR content:\"" + querystr +
											"\"", Answer.class);
    	
    	courseQuery.all(); 
    	lectureQuery.all();
    	commentQuery.all();
    	userQuery.all();
    	questionQuery.all();
    	answerQuery.all();
    	
    	List<Course> courseResults = courseQuery.fetch();
    	List<Lecture> lectureResults = lectureQuery.fetch();
    	List<Comment> commentResults = commentQuery.fetch();
    	List<User> userResults = userQuery.fetch();
    	List<Question> questionResults = questionQuery.fetch();
    	List<Answer> answerResults = answerQuery.fetch();
    	
    	render(courseResults, lectureResults, commentResults, userResults, questionResults, answerResults);
    }
    
    static void redirectToOriginalURL() throws Throwable {
        String url = flash.get("url");
        if(url == null) {
            url = "/";
        }
        redirect(url);
    }

}
